<?php
class Model_Validator
{
    public static function email($email)
    {
        $email = trim($email);
        $filter = '/^([a-zA-Z0-9_.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/';
        if($email == '' || !preg_match($filter,$email)){
            return FALSE;
        }
        
        return TRUE;
    }
    
    public static function userName($userName)
    {
        if(strlen($userName) < 1 || strlen($userName) > 50){
            return FALSE;
        }
        
        $filter = '/^[a-zA-Z0-9 ]+$/';
        if(!preg_match($filter,$userName)){
            return FALSE;
        }
        
        return TRUE;
    }
    
    public static function password($password)
    {
        $password = trim($password);
        if($password == '' || strlen($password) < 3){
            return FALSE;
        }
        
        return TRUE;
    }
    
    public static function level($levelID)
    {
        $levelID = intval($levelID);
        $levelInfo = Config_Default::getAuthConfig();
        
        if(!isset($levelInfo[$levelID])){
            return false;
        }  
        
        return true;
    }
    
    public static function timeYMD($formate)
    {
        $filter = '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/';
        if(!preg_match($filter,$formate)){
            return FALSE;
        }
        
        $formate = $formate . " 00:00:00";
        $after = date('Y-m-d H:i:s', strtotime($formate));
        if($after != $formate){
            return false;
        }
        
        return true;
    }
    
    public static function timeYM($formate)
    {
        $filter = '/^[0-9]{4}-[0-9]{2}$/';
        if(!preg_match($filter, $formate)){
            return FALSE;
        }
        
        $formate = $formate . "-01 00:00:00";
        $after = date('Y-m-d H:i:s', strtotime($formate));
        if($after != $formate){
            return false;
        }
        
        return true;
    }
    
    public static function timeYMDSFM($formate)
    {
        $filter = '/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/';
        if(!preg_match($filter,$formate)){
            return FALSE;
        }
        $formate = $formate;
        $after = date('Y-m-d H:i:s', strtotime($formate));
        if($after != $formate){
            return false;
        }
        
        return true;
    }
    
    public static function price($price)
    {
        $filter = '/^[-]{0,1}[0-9]+$/';
        if(!preg_match($filter,$price)){
            return FALSE;
        }
        
        return true;
    }
    
    public static function tel($tel)
    {
        $filter = '/^[0-9]{11}$/';
        if(!preg_match($filter,$tel)){
            return FALSE;
        }
        
        return true;
    }
    
    public static function picture($name)
    {
        $nameArr = explode(".", $name);
        if(count($nameArr) <= 1){
            return false;
        }
        
        $lastName = strtolower(end($nameArr));
        
        if($lastName != "jpg" && $lastName != "png" && $lastName != "jpeg" && $lastName != "gif" && $lastName != "bmp"){
            return false;
        }
        
        return true;
    }
    
    public static function modelStyle($id)
    {
    	if(Model_Model::getModelStyleName($id)){
    		return true;
    	}
    	
    	return false;
    }
}
